package com.acg.auth.controller;


import com.acg.auth.pojo.News;
import com.acg.auth.pojo.SysRole;
import com.acg.auth.pojo.SysUser;
import com.acg.auth.service.NewsService;
import com.acg.auth.vo.NewsVo;
import com.acg.exception.ACGException;
import com.acg.util.Result;
import com.acg.util.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author acg
 * @since 2025-08-08
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/acg/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @GetMapping("/getAll")
    @ApiOperation(value = "查找全部")
    public Result getAll() throws ACGException {

        // 模拟异常抛出
        //throw new TogogoException("异常测试", 20001);

        // 查找全部
        List<News> list = newsService.list();
        // 构建统一返回结果
        return Result.ok(list);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据主键进行查找")
    public Result getById(@PathVariable Long id) {
        // 根据主键进行查找
        News byId = newsService.getById(id);
        // 判断 byid是否为空
        if (!ObjectUtils.isEmpty(byId)) {

            return Result.ok(byId);
        } else {

            return Result.fail();
        }
    }

    // 根据 typeId 查找不同类型的资讯
    @GetMapping("/getByTypeId/{id}")
    public Result getByTypeId(@PathVariable Long id){
        List<News> newsList = newsService.getByTypeId(id);
        return Result.ok(newsList);
    }

    // 根据作id查询所有的 资讯
    @GetMapping("/user/{id}")
    public Result getNewsByUserId(@PathVariable Long id){
        List<News> newsList = newsService.selectByUserId(id);
        return Result.ok(newsList);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/getPage/{page}/{limit}")
    public Result getPage(@PathVariable Long page , @PathVariable Long limit, NewsVo vo) {

        System.out.println("limit"+limit +" page"+page);

        // 条件判断
        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
        lqw.like(News::getTitle, vo.getTitle())
                .like(News::getAuthor, vo.getAuthor())
                .like(News::getStatus, vo.getStatus());


        // 1、 构建分页信息  第几页  每页显示
        Page<News> newsPage = new Page<>(page, limit);

        System.out.println("rolePage"+newsPage);

        // 2、 查找分页信息
        Page<News> newsPaged = newsService.page(newsPage,lqw);
        return Result.ok(newsPaged);
    }


    @PostMapping("/save")
    public Result save(@RequestBody News news) {
        // 1、新增数据
        news.setCreateTime(LocalDateTime.now());
        news.setUpdateTime(LocalDateTime.now());
        boolean save = newsService.save(news);

        if (save) {
            return Result.ok();
        }else {
            return Result.build("新增失败", ResultCodeEnum.SAVE_ERROR);
        }

    }

    @ApiOperation(value = "更新数据")
    @PutMapping("/update")
    public Result update(@RequestBody News news) {
        // 1、新增数据
        boolean update = newsService.updateById(news);
        if (update) {
            return Result.ok();
        }else {
            return Result.build("更新失败", ResultCodeEnum.UPDATE_ERROR);
        }
    }

    /**
     * 删除
     *  id  主键
     * @return
     */
    @ApiOperation(value = "根据主键删除数据")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        // 1、删除数据
        boolean remove = newsService.removeById(id);
        if (remove) {
            return Result.ok();
        }else {
            return Result.build("删除失败", ResultCodeEnum.UPDATE_ERROR);
        }
    }


    /**
     * 批量删除
     * @param ids  主键 列表
     * @return
     */
    @ApiOperation(value = "批量删除数据")
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody List<Long> ids) {
        // 1、删除数据
        boolean remove = newsService.removeByIds(ids);
        if (remove) {
            return Result.ok();
        }else {
            return Result.build("删除失败", ResultCodeEnum.UPDATE_ERROR);
        }
    }

}

